const Model = require('./model')
const Tool = require('@/lib/tool/YBTool')
const BaseMongo = require('../base/index')

/**
 * 用户Mongomo模型
 */
class AdminMongo extends BaseMongo{

	constructor(){
		super()
	}

	// =================================== admin ================================================
	/**
	 * 更新用户
	 * @param data 需要更新的数据
	 * @param uid 用户ID
	 * @returns {Promise<void>}
	 */
	static updateAdmin (data, uid) {
		const p = {
			user: data.user,
			icon: data.icon
		}
		if (data.pass) {
			const MD5Pass = Tool.MD5(data.pass)
			p.pass = MD5Pass
		}
		return super.updateOneWithId(uid, p)
	}

	/**
	 * 设置登录错误次数
	 * @param userModel 用户模型
	 * @returns {Promise<void>}
	 */
	static addLoginErrNum(userModel) {
		const num = userModel.loginErrNum + 1
		return Model.updateOne(
			{_id: this.objId(userModel._id)},
			{loginErrNum: num})
	}

	/**
	 * 清除计数
	 */
	static clearLoginErrNum(userModel) {
		return Model.updateOne(
			{_id: this.objId(userModel._id)},
			{
				loginErrNum: 0
			})
	}
}

/**
 * 设置当前模型
 */
AdminMongo.model = Model

module.exports = AdminMongo
